﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SwitchMiner.Interfaces;
using SwitchMiner.Models;

namespace SwitchMiner
{
    public class MinersControler
    {
        private List<MinerModel> _miners = null;
        private IModelNotification _newMinerCallback = null;

        public MinersControler(IModelNotification minercallback, List<MinerModel> minerModel)
        {
            _miners         = minerModel;
            _newMinerCallback = minercallback;
            if (_miners.Count > 0 && _newMinerCallback != null)
            {
                _newMinerCallback.MinerModelsChanged(_miners);
            }
        }

        public List<MinerModel> GetModel()
        {
            return _miners;
        }
        public void AddMiner(MinerModel model)
        {
            if (_miners != null)
            {
                if (_miners.Contains(model) == false)
                {
                    foreach (MinerModel innerModel in _miners)
                    {
                        if (innerModel.MinerName == model.MinerName)
                        {
                            return;
                        }
                    }
                    _miners.Add(model);
                    if (null != _newMinerCallback)
                    {
                        _newMinerCallback.MinerModelsChanged(_miners);
                    }
                }
            }
        }
        public void RemoveMiner(MinerModel model)
        {
            if (_miners != null)
            {
                if (_miners.Contains(model) == true)
                {
                    _miners.Remove(model);
                    if (null != _newMinerCallback)
                    {
                        _newMinerCallback.MinerModelsChanged(_miners);
                    }
                }
            }
        }

        public MinerModel GetModelFromName(string minerName)
        {
            foreach (MinerModel model in _miners)
            {
                if (model.MinerName == minerName)
                {
                    return model;
                }
            }
            return null;
        }
    }
}
